package com._520it.crm.web.controller;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by 呆 on 2017/10/16.
 */
@ControllerAdvice
public class ExceptionHanlder {
    @ExceptionHandler(UnauthorizedException.class)//UnauthorizedException.class
    public void handlerException(HandlerMethod handlerMethod, HttpServletResponse response){
        try {
            if(handlerMethod.getMethodAnnotation(ResponseBody.class)!=null){
                response.setCharacterEncoding("utf-8");
                response.getWriter().write("{\"success\":false,\"msg\":\"您没有权限访问!\",total:0,rows:[]}");
            }else {
                response.sendRedirect("/nopermission.jsp");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
